package com.yr.ryjk.ads.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.ads.dao.AdsMapper;
import com.yr.ryjk.ads.entity.Ads;
import com.yr.ryjk.common.Constant;
import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.param.dao.ParamMapper;
import com.yr.ryjk.util.IDGenerator;

@Service
public class AdsService implements IAdsService {
	@Autowired
	private AdsMapper adsMapper;
	@Autowired
	private ParamMapper paramMapper;

	@Override
	public void add(Ads ads) {
		ads.setId(IDGenerator.gen32UUID());
		ads.setAddTime(new Date().getTime());
		adsMapper.insert(ads);
	}

	@Override
	public void delete(String[] classList) {
		adsMapper.deleteByPrimaryKey(classList);
	}

	@Override
	public void modify(String id, String type, String picUrl, String externalUrl) {
		Ads ads = new Ads();
		ads.setId(id);
		ads.setType(type);
		ads.setAddTime(new Date().getTime());
		ads.setExternalUrl(externalUrl);
		ads.setPicUrl(picUrl);
		adsMapper.updateByPrimaryKeySelective(ads);
	}

	@Override
	public Map<String, Object> getListOfPage(int pageNo, int pageSize, String type) {
		List<Ads> list = adsMapper.selectOfPage((pageNo - 1) * pageSize,pageSize, type);
		int count = adsMapper.selectCount(type);
		
		Pagination page = new Pagination();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setTotalCount(count);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", page);
		return map;
	}

	@Override
	public List<Ads> getListOfType(String type) {
		Integer count = Integer.parseInt(paramMapper.selectCountByKey(type + "_ads"));
		count = (count == null ? Constant.ads_default_count : count);
		return adsMapper.selectOfType(type, count);
	}
	
	@Override
	public void updateRefreshTime(String id) {
		adsMapper.updateTime(id, new Date().getTime());
	}

	@Override
	public Ads getAdsById(String id) {
		return adsMapper.selectByPrimaryKey(id);
	}
}